package com.popay.xxljob;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Slf4j
@Component
@EnableDiscoveryClient
//@EntityScan("com.popay.*.entity")
//@MapperScan(basePackages = {"com.popay.*.dao"})
@ComponentScan(basePackages = "com.popay.*")
@SpringBootApplication
public class XXLJobApplication implements ApplicationListener<WebServerInitializedEvent> {

	public static void main(String[] args) {
		SpringApplication.run(XXLJobApplication.class, args);
	}

	@SneakyThrows
	@Override
	public void onApplicationEvent(WebServerInitializedEvent event) {
		WebServer server = event.getWebServer();
		WebServerApplicationContext context = event.getApplicationContext();
		Environment env = context.getEnvironment();
		String ip = InetAddress.getLocalHost().getHostAddress();
		int port = server.getPort();
		String contextPath = env.getProperty("server.servlet.context-path");
		if (contextPath == null) {
			contextPath = "";
		}
		String agreement = env.getProperty("server.ssl.key-store");
		if (agreement == null) {
			agreement = "http";
		} else {
			agreement = "https";
		}
		log.info("\n---------------------------------------------------------\n" +
				"\tApplication is running! Access address:\n" +
				"\tLocal:\t\t {}://localhost:{}" +
				"\n\tExternal:\t {}://{}:{}{}" +
				"\n\tSwagger2 Api Doc:\t{}://{}:{}{}/swagger-ui.html#/" +
				"\n\tknife4j Api Doc:\t{}://{}:{}{}/doc.html#/" +
				"\n---------------------------------------------------------\n", agreement, port, agreement, ip, port, contextPath, agreement, ip, port, contextPath, agreement, ip, port, contextPath);
	}
}
